package com.lanou.dllo.vc.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.dllo.vc.MyApplication;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.activity.BindingPhonoActivity;
import com.lanou.dllo.vc.activity.LoginActivity;
import com.lanou.dllo.vc.activity.NewsCuesActivity;
import com.lanou.dllo.vc.activity.Personal_data_Activity;
import com.lanou.dllo.vc.activity.SettingUserActivty;

/**
 * Created by dllo on 15/11/16.
 */
public class SettingFragment extends Fragment implements View.OnClickListener {
    private TextView personal, binding, newsCues, clearCacheTv;
    private Button exit;
    private String username;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }


    /**
     * 在这个方法里面，获取传入的数据
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    /**
     * 这个方法里面加载布局
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_install, null);
        return view;
    }


    /**
     * 控件初始化在这写
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        personal = (TextView) view.findViewById(R.id.personal);
        personal.setOnClickListener(this);
        exit = (Button) view.findViewById(R.id.exit);
        exit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }

        });
        binding = (TextView) view.findViewById(R.id.binding);
        binding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BindingPhonoActivity.class);
                startActivity(intent);
            }
        });
        newsCues = (TextView) view.findViewById(R.id.newsCues);
        newsCues.setOnClickListener(this);

        clearCacheTv = (TextView) view.findViewById(R.id.clearCacheTv);
        clearCacheTv.setOnClickListener(this);

    }


    /**
     * 数据初始化在这写
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.personal:
                Intent intent = new Intent(getActivity(), SettingUserActivty.class);
                username = MyApplication.getInstance().getUserName();
                intent.putExtra("username",username);
                startActivity(intent);
            break;

            case R.id.newsCues:
                Intent intent_newsCues = new Intent(getActivity(), NewsCuesActivity.class);
                startActivity(intent_newsCues);
                break;
            case R.id.clearCacheTv:
                clearCache();
                break;
        }
    }

    private void clearCache() {
        getActivity().deleteDatabase("vc");//删除数据库
        Toast.makeText(getActivity(),"数据库已清空",Toast.LENGTH_SHORT).show();


    }
}
